<?php

class Admin_model extends CI_Model
{
	function loginAdmin($username,$password)
	{	
		$this->load->helper('security');
		//$hashPassword=do_hash($password);
		
		$sqlCommand='SELECT * FROM admin WHERE username = ? AND password = ?';
		$result=$this->db->query($sqlCommand,array($username,$password));
		
		if ($result->num_rows()==1)
		{	
			return $result->row_object(0);
		}
		else
		{
			return FALSE;
		}		
	}
	
	
	function getAllUsers()
	{
		
		$sqlCommand='SELECT * FROM kund';
		$result=$this->db->query($sqlCommand);
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}		
		
		
		
		
	}
	
	function getSearchUser($email)
	{
		
		$sqlCommand='SELECT * FROM kund WHERE email LIKE ?';
		$search = "%" . $email . "%";
		
		
		$result=$this->db->query($sqlCommand, array($search));
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}		
		
		
		
	}
	
	function getAllBookings()
	{
		$sqlCommand='SELECT * FROM bokning ORDER BY StartTid DESC';
		$result=$this->db->query($sqlCommand);
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}		
		
		
	}
	
	function searchBookings($email)
	{
		$search = "%" . $email . "%";
		$sqlCommand='SELECT * FROM bokning JOIN kund ON bokning.KundID = kund.ID WHERE kund.Email LIKE ? ORDER BY StartTid DESC';
		$result=$this->db->query($sqlCommand, array($search));
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}		
	}
	
	
	function getAllCars()
	{		
		//Funktion som hämtar alla bilar i databasen.
		$sqlCommand='SELECT * FROM bil ORDER BY ID';
		$result=$this->db->query($sqlCommand);
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}	
	}
	
	function getACar($id)
	{
		//function that gets one car with the ID of the car
		
		$sqlCommand='SELECT * FROM bil WHERE ID= ?';
		$result=$this->db->query($sqlCommand, array($id));
		
		if ($result->num_rows()==1)
		{	
			return $result->row_object(0);
			
		}
		else
		{
			return FALSE;
		}	
		
	}
	
		function getAllPriceClasses()
	{		
		//Funktion som hämtar en prsiklass från databasen.
		$sqlCommand='SELECT * FROM prisklass ORDER BY ID';
		$result=$this->db->query($sqlCommand);
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}	
	}
	
	function getPriceClassDetails($priceClass)
	{
		//
		
		//Function that gets data about the prices for each car with a specific priceclass
		$sqlCommand='SELECT * FROM prisklass JOIN bilpris ON prisklass.ID = bilpris.PrisklassID JOIN bil on bilpris.BilID = bil.ID WHERE prisklass.ID = ?';
		$result=$this->db->query($sqlCommand, array($priceClass));
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}	
	}
	
	
	
	
	function savePriceClassName($id, $name)
	{
			$sqlCommand='UPDATE prisklass SET Namn= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($name, $id));
		
			return $query;
	}
	
	
	function savePriceClassInfo($id, $info)
	{
			$sqlCommand='UPDATE prisklass SET Forklaring= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($info, $id));
		
			return $query;
	}
	

	function insertOneUseDiscount($code, $discount)
	{
		//creating a new row in rabatt and one use rabatt		
		$sqlCommand='INSERT INTO rabatt (`kod`, `rabatt`) VALUES (?, ?)';
		
		$result=$this->db->query($sqlCommand,array($code, $discount));
		
		if ($result ==1)
		{	
			
			$sqlCommand='SELECT * FROM rabatt WHERE kod = ?';
			$disc = $this->db->query($sqlCommand,array($code));
			
			
			
			if ($disc->num_rows()==1)
			{	
				$discObject = $disc->row_object(0);
				
				
				
				
					$sqlCommand='INSERT INTO oneuserabatt (`RabattID`, `annvand`) VALUES (?, ?)';
		
					$result=$this->db->query($sqlCommand,array($discObject->ID, 0));
				
				
					return $result;
			}
			else
			{
				return FALSE;
				
				
			}	
			
			
			
		}
		else
		{
			return FALSE;
		}
	}
	
	
	
	
	function insertPeriodDiscount($code, $discount, $startDate, $endDate)
	{
		//creating a new row in rabatt and one use rabatt		
		$sqlCommand='INSERT INTO rabatt (`kod`, `rabatt`) VALUES (?, ?)';
		
		$result=$this->db->query($sqlCommand,array($code, $discount));
		
		if ($result ==1)
		{	
			
			$sqlCommand='SELECT * FROM rabatt WHERE kod = ?';
			$disc = $this->db->query($sqlCommand,array($code));
			
			
			
			if ($disc->num_rows()==1)
			{	
				$discObject = $disc->row_object(0);
				
				
				
				
					$sqlCommand='INSERT INTO periodsrabatt (`RabattID`, `StartDatum`, `SlutDatum`) VALUES (?, ?, ?)';
		
					$result=$this->db->query($sqlCommand,array($discObject->ID, $startDate, $endDate));
				
				
					return $result;
			}
			else
			{
				return FALSE;
				
				
			}	
			
			
			
		}
		else
		{
			return FALSE;
		}
	}
	
	
	function getAllOneUseDiscounts()
	{
		
		
				//Function that gets and returns all the one use discounts in the database
		$sqlCommand='Select * from rabatt JOIN oneuserabatt ON rabatt.ID = oneuserabatt.RabattID ORDER BY annvand';
		$result=$this->db->query($sqlCommand);
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}	
	}
	
	
	function getAllPeriodDiscounts()
	{
					//Function that gets and returns all the period discounts in the database
		$sqlCommand='Select * from rabatt JOIN periodsrabatt ON rabatt.ID = periodsrabatt.RabattID ORDER BY StartDatum DESC';
		$result=$this->db->query($sqlCommand);
		
		if ($result->num_rows()>=1)
		{	
			return $result->result_array();
			
		}
		else
		{
			return FALSE;
		}	
	}
	
	
	function RegisterAgreementUser($username,$password,$firstName,$lastName,$personNumber,$adress,$telNumber,$email,$organisationName,$priceClass,$confirmCode)
	{	
	
		$this->load->helper('security');
		$hashPassword=do_hash($password);
		 
		//	$sqlCommand='INSERT INTO kund VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
	
		
		$sqlCommand='INSERT INTO kund (`username`, `password`, `Fnamn`, `Enamn`, `Personnummer`, `Adress`, `TelNummer`, `Email`,`aktiv`, `PrisklassID`, `confirmcode`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?)';
		
		$result=$this->db->query($sqlCommand,array($username,$hashPassword,$firstName,$lastName,$personNumber,$adress,$telNumber,$email,1, $priceClass, $confirmCode));
		
		if ($result ==1)
		{	
			$sqlCommand='SELECT ID from kund WHERE email = ?';
			$theUser=$this->db->query($sqlCommand,array($email));
			$userID = $theUser->row_object();
			
			if($result != '')
			{
				$sqlCommand='INSERT INTO avtalskund (`KundNr`, `Organisationsnamn`) VALUES (?, ?)';
		
				$result=$this->db->query($sqlCommand,array($userID->ID, $organisationName));
				
				if ($result ==1)
				{
					return $result;
				}
				
				else
				{
					return FALSE; 
				}
			}
			
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	
	}
}